/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
 
#ifndef GAUTOCLIENT_H
#define GAUTOCLIENT_H

// Qt
#include <QTcpSocket>
#include <QPixmap>

// local
#include "autopackets.h"
#include "analyzer.h"    // for pimage_t 

/// ICud-implementation proxy. Inherits ICud interface, and tarnsits call to actual object vai TCP
class GAutoClient : public QObject
{
	Q_OBJECT
			
public:

	// construction/destruction
	GAutoClient();
	virtual ~GAutoClient();

	// --- network conection operations ---

	/// Conencts to remote host
	void connectToHost( const QString & hostName, quint16 port = 5102 );

	/// Disconects from remote host
	void disconnectFromHost();

	/// checks if conection is established
	bool isConnected();

	// --- Camera Specific ---

	/// Returns next frame
	QPixmap frame();
	
	/// Sends xml config file to server.
	void sendXmlConfig( QByteArray & fileData );
	
	/// Sends markers analyzer image to server.
	void sendMarkers( pimage_t markers );
	
	/// Sends roi analyzer image to server.
	void sendRoi( pimage_t roi );
	
	/// Sends params to server.
	void sendParams( const QVariantMap & params );
	
public slots:
	/// \brief If socket disconnectes emits signal.
	void socketStateChanged( QAbstractSocket::SocketState socketState );
	
signals:
	void disconnected();
	
private:

	QTcpSocket	_socket;		///< comm socket
	int 		_timeout;		///< Allowable timeout [ms]

	quint32 		_requestId;	///< USed to calculate next request id

	quint32 getRequestId();		///< Returns session-unique request id
	
	/// Waits for reply packet
	GAutoReply * waitForReply( quint32 requestId );

	/// Waits for packet
	GAutoPacket * waitForPacket();

	/// sends packet
	void sendPacket( const GAutoPacket& packet );


};

#endif // !GAUTOCLIENT_H

// eof
